﻿Imports System.Windows.Forms

Public Class Generica
    Public Shared Function MaskNumber(ByVal Numero As String, Optional ByVal Decimales As Integer = 0) As String
        Return FormatNumber(Numero, Decimales, , , TriState.True)
    End Function

    Public Shared Function Num2Text(ByVal value As Double) As String
        Select Case value
            Case 0 : Num2Text = "CERO"
            Case 1 : Num2Text = "UN"
            Case 2 : Num2Text = "DOS"
            Case 3 : Num2Text = "TRES"
            Case 4 : Num2Text = "CUATRO"
            Case 5 : Num2Text = "CINCO"
            Case 6 : Num2Text = "SEIS"
            Case 7 : Num2Text = "SIETE"
            Case 8 : Num2Text = "OCHO"
            Case 9 : Num2Text = "NUEVE"
            Case 10 : Num2Text = "DIEZ"
            Case 11 : Num2Text = "ONCE"
            Case 12 : Num2Text = "DOCE"
            Case 13 : Num2Text = "TRECE"
            Case 14 : Num2Text = "CATORCE"
            Case 15 : Num2Text = "QUINCE"
            Case Is < 20 : Num2Text = "DIECI" & Num2Text(value - 10)
            Case 20 : Num2Text = "VEINTE"
            Case Is < 30 : Num2Text = "VEINTI" & Num2Text(value - 20)
            Case 30 : Num2Text = "TREINTA"
            Case 40 : Num2Text = "CUARENTA"
            Case 50 : Num2Text = "CINCUENTA"
            Case 60 : Num2Text = "SESENTA"
            Case 70 : Num2Text = "SETENTA"
            Case 80 : Num2Text = "OCHENTA"
            Case 90 : Num2Text = "NOVENTA"
            Case Is < 100 : Num2Text = Num2Text(Int(value \ 10) * 10) & " Y " & Num2Text(value Mod 10)
            Case 100 : Num2Text = "CIEN"
            Case Is < 200 : Num2Text = "CIENTO " & Num2Text(value - 100)
            Case 200, 300, 400, 600, 800 : Num2Text = Num2Text(Int(value \ 100)) & "CIENTOS"
            Case 500 : Num2Text = "QUINIENTOS"
            Case 700 : Num2Text = "SETECIENTOS"
            Case 900 : Num2Text = "NOVECIENTOS"
            Case Is < 1000 : Num2Text = Num2Text(Int(value \ 100) * 100) & " " & Num2Text(value Mod 100)
            Case 1000 : Num2Text = "MIL"
            Case Is < 2000 : Num2Text = "MIL " & Num2Text(value Mod 1000)
            Case Is < 1000000 : Num2Text = Num2Text(Int(value \ 1000)) & " MIL"
                If value Mod 1000 Then Num2Text = Num2Text & " " & Num2Text(value Mod 1000)
            Case 1000000 : Num2Text = "UN MILLON"
            Case Is < 2000000 : Num2Text = "UN MILLON " & Num2Text(value Mod 1000000)
            Case Is < 1000000000000.0# : Num2Text = Num2Text(Int(value / 1000000)) & " MILLONES "
                If (value - Int(value / 1000000) * 1000000) Then Num2Text = Num2Text & " " & Num2Text(value - Int(value / 1000000) * 1000000)
            Case 1000000000000.0# : Num2Text = "UN BILLON"
            Case Is < 2000000000000.0# : Num2Text = "UN BILLON " & Num2Text(value - Int(value / 1000000000000.0#) * 1000000000000.0#)
            Case Else : Num2Text = Num2Text(Int(value / 1000000000000.0#)) & " BILLONES"
                If (value - Int(value / 1000000000000.0#) * 1000000000000.0#) Then Num2Text = Num2Text & " " & Num2Text(value - Int(value / 1000000000000.0#) * 1000000000000.0#)
        End Select


    End Function

    Public Shared Function GetMeses() As DataTable
        Dim dt As New DataTable

        dt.Columns.Add("codigo")
        dt.Columns.Add("Nombre")

        Dim row As DataRow

        row = dt.NewRow
        row(0) = "01"
        row(1) = "Enero"

        dt.Rows.Add(row)

        row = dt.NewRow
        row(0) = "02"
        row(1) = "Febrero"

        dt.Rows.Add(row)

        row = dt.NewRow
        row(0) = "03"
        row(1) = "Marzo"
        dt.Rows.Add(row)

        row = dt.NewRow
        row(0) = "04"
        row(1) = "Abril"
        dt.Rows.Add(row)

        row = dt.NewRow
        row(0) = "05"
        row(1) = "Mayo"
        dt.Rows.Add(row)
        row = dt.NewRow

        row(0) = "06"
        row(1) = "Junio"
        dt.Rows.Add(row)

        row = dt.NewRow
        row(0) = "07"
        row(1) = "Julio"
        dt.Rows.Add(row)

        row = dt.NewRow
        row(0) = "08"
        row(1) = "Agosto"
        dt.Rows.Add(row)

        row = dt.NewRow
        row(0) = "09"
        row(1) = "Septiembre"
        dt.Rows.Add(row)

        row = dt.NewRow
        row(0) = "10"
        row(1) = "Octubre"
        dt.Rows.Add(row)

        row = dt.NewRow
        row(0) = "11"
        row(1) = "Noviembre"
        dt.Rows.Add(row)

        row = dt.NewRow
        row(0) = "12"
        row(1) = "Diciembre"
        dt.Rows.Add(row)

        Return dt

    End Function


    Public Shared Function GetAnios(ByVal inicio As Integer, ByVal fin As Integer) As DataTable
        Dim dt As New DataTable
        dt.Columns.Add("anio")

        Dim i As Integer

        For i = inicio To fin
            Dim row As DataRow
            row = dt.NewRow
            row(0) = i
            dt.Rows.Add(row)
        Next
        Return dt

    End Function

    Public Shared Function GetAppConfig(ByVal Key As String) As String
        Try
            Return System.Configuration.ConfigurationManager.AppSettings(Key).ToString
        Catch ex As Exception
            Return ""
        End Try

    End Function

    Public Shared Function GetSQLConfig(ByVal Key As String, ByVal cnnString As String, Optional ByVal cacheable As Boolean = True) As String
        Try
            Dim i As Integer
            If cacheable Then
                If Not CacheNombre Is Nothing Then
                    For i = 0 To CacheNombre.Length - 1
                        If CacheNombre(i) = Key And CacheTime(i) >= Now Then
                            Return CacheValue(i)
                            Exit For
                        End If
                    Next
                End If
            End If



            Dim exec As New DotNetDal.SQLPersistence(False, "")
            Dim rp As New DotNetResponse.SQLPersistence("")
            rp.StoredProcedure = "PA_App_Config"
            rp.AddParametro(Key)
            exec.ExecuteDataTable(rp)
            If rp.TieneDatos Then
                If cacheable Then
                    If CacheNombre Is Nothing Then
                        ReDim CacheNombre(0)
                        ReDim CacheTime(0)
                        ReDim CacheValue(0)
                        CacheNombre(0) = Key
                        CacheTime(0) = Now.AddMinutes(120)
                        CacheValue(0) = rp.DtTable.Rows(0).Item(0)
                    Else
                        ReDim Preserve CacheNombre(CacheNombre.Length)
                        ReDim Preserve CacheTime(CacheNombre.Length)
                        ReDim Preserve CacheValue(CacheNombre.Length)

                        CacheNombre(CacheNombre.Length - 1) = Key
                        CacheTime(CacheNombre.Length - 1) = Now.AddMinutes(120)
                        CacheValue(CacheNombre.Length - 1) = rp.DtTable.Rows(0).Item(0)

                    End If
                End If

                Return rp.DtTable.Rows(0).Item(0)
            Else
                Return ""
            End If
            'Return Configuration.ConfigurationManager.AppSettings(Key).ToString
        Catch ex As Exception
            Return ""
        End Try

    End Function

    Public Shared Function GetFechayyyymmdd(ByVal fecha As DateTime) As String
        Dim dia As String = fecha.Day
        Dim mes As String = fecha.Month
        Dim anio As String = fecha.Year

        If dia.Length = 1 Then
            dia = "0" & dia
        End If

        If mes.Length = 1 Then
            mes = "0" & mes
        End If
        Return anio & mes & dia


    End Function

    Public Shared Function RutDigito(ByVal Rut As Integer) As String
        Dim Valida As String
        Dim Digito As Integer
        Dim Contador As Integer
        Dim Multiplo As Integer
        Dim Acumulador As Integer
        'Valida = Val
        Contador = 2
        Acumulador = 0
        While Rut <> 0
            Multiplo = (Rut Mod 10) * Contador
            Acumulador = Acumulador + Multiplo
            Rut = Rut \ 10
            Contador = Contador + 1
            If Contador = 8 Then
                Contador = 2
            End If
        End While
        Digito = 11 - (Acumulador Mod 11)
        RutDigito = CStr(Digito)
        If Digito = 10 Then RutDigito = "K"
        If Digito = 11 Then RutDigito = "0"

        Return RutDigito
    End Function

    Public Shared Function SaveEventViewer(ByVal sSource As String, ByVal TipoError As EventLogEntryType, ByVal sEvent As String, Optional ByVal sLog As String = "Application")
        'Dim sSource As String
        'Dim sLog As String
        'Dim sEvent As String
        Dim sMachine As String
        'sSource = "dotNET Sample App"
        'sLog = "Application"
        'sEvent = "Sample Event"
        sMachine = "."
        Dim source As New EventSourceCreationData(sSource, sLog)
        If Not EventLog.SourceExists(sSource, sMachine) Then
            System.Diagnostics.EventLog.CreateEventSource(source) 'sSource, sLog, sMachine)
        End If


        Dim ELog As New EventLog(sLog, sMachine, sSource)
        ELog.WriteEntry(sEvent, TipoError, 234, CType(3, Short))

    End Function

    Public Shared Sub DatagridClear(ByRef Dg As DataGridView)
        Dim i As Integer
        Dim j As Integer = Dg.Rows.Count - 1
        Dim ct As Integer = Dg.Rows.Count

        For i = 0 To ct - 1
            Dg.Rows.Remove(Dg.Rows(j))
            j = j - 1
        Next
    End Sub


End Class
